package cn.edu.neusoft.zengweidong707.neufood.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import cn.edu.neusoft.zengweidong707.neufood.R;
import cn.edu.neusoft.zengweidong707.neufood.activity.LoginActivity;
import cn.edu.neusoft.zengweidong707.neufood.activity.UpdateUserInfoActivity;
import cn.edu.neusoft.zengweidong707.neufood.activity.UserCommentActivity;
import cn.edu.neusoft.zengweidong707.neufood.activity.UserOrderActivity;
import cn.edu.neusoft.zengweidong707.neufood.bean.UserByIdBean;
import cn.edu.neusoft.zengweidong707.neufood.listener.ZwdListener;
import cn.edu.neusoft.zengweidong707.neufood.model.UserModel;


public class MeFragment extends BaseFragment {
    private TextView user_name;
    private TextView user_address;
    private TextView user_phone;
    private TextView my_comment;
    private TextView my_order;
    private ZwdListener<UserByIdBean> listener;
    private UserModel model;
    private Button updata;
    private Button btn_exit;
    private int userid;
    private String username, userpass, mobilenum, address, comment;

    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        setLayout_file(R.layout.fragment_me);

        listener = new ZwdListener<UserByIdBean>() {
            @Override
            public void onResponse(UserByIdBean l) {
                if(l != null) {
                    user_name.setText(l.getUsername());
                    user_address.setText(l.getAddress());
                    user_phone.setText(l.getMobilenum());

                    userid = l.getUser_id();
                    username = l.getUsername();
                    userpass = l.getUserpass();
                    mobilenum = l.getMobilenum();
                    address = l.getAddress();
                    comment = l.getComment();
                }
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
            }
        };

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    void initView() {
        updata = (Button) view.findViewById(R.id.updata);
        user_name = (TextView) view.findViewById(R.id.user_name);
        user_address = (TextView) view.findViewById(R.id.user_address);
        user_phone = (TextView) view.findViewById(R.id.user_phone);
        my_order = (TextView) view.findViewById(R.id.my_order);
        my_comment = (TextView) view.findViewById(R.id.my_comment);
        btn_exit = (Button) view.findViewById(R.id.btn_exit);
    }

    @Override
    void initEvent() {
        my_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), UserOrderActivity.class);
                startActivity(intent);
            }
        });

        my_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), UserCommentActivity.class);
                startActivity(intent);
            }
        });

        updata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), UpdateUserInfoActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("username", username);
                bundle.putString("mobilenum", mobilenum);
                bundle.putString("address", address);
                //bundle.putString("userpass", userpass);
                bundle.putString("comment", comment);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        btn_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    void initData() {
        int uid = Integer.parseInt(getUserid().toString());
        model = new UserModel();
        model.getUserInfo(uid, listener);
    }

    public MeFragment() {
        // Required empty public constructor
    }


    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
